package com.axcbd.demo.service;


import com.axcbd.demo.model.CameraInformation;
import com.axcbd.demo.repository.CameraInformationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CameraInformationService {

    @Autowired
    private CameraInformationRepository cameraInformationRepository;

    //格式化日期工具类
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public void saveCameraInformation(int cameraid, String imageUrl){
        CameraInformation info = new CameraInformation();
        info.setCameraid(cameraid);
        info.setCreatedat(new Date());
        info.setImageurl(imageUrl);
        info.setInformation("Motion detected and frame saved");

        cameraInformationRepository.save(info);
    }

    public List<CameraInformation> getCameraInformationByCameraid(int cameraid){
        return cameraInformationRepository.findByCameraid(cameraid);
    }

    public List<CameraInformation> getCameraInformationByDateRange(int cameraid,String date) throws ParseException {
        Date startOfDay = sdf.parse(date);
        Date endOfDay = new Date(startOfDay.getTime()  +23*3600*1000 +59*60*1000 +59*1000);
        return cameraInformationRepository.findByDateRange(cameraid,startOfDay,endOfDay);
    }


}
